<?php
/**
 * 这里是系统session处理函数
 */

if($sys_conf_var['session']['save_type'] == 2)
{	
	function _sess_open($save_path, $session_name) 
	{
		return true;
	}

	function _sess_close() 
	{
		return true;
	}

	function _sess_read($key) 
	{
		global $db,$sys_conf_var;
		
		try 
		{
			$qid = "select value
			from " . $sys_conf_var['table']['session'] . "
			where sesskey = '" . $key . "'
			and expiry > '" . time() . "'";
		
			$value = $db->Execute($qid);

			if (isset($value->fields['value']) && $value->fields['value']) {
				return $value->fields['value'];
			}
	
			return ("");
		}
		catch (MySqlException $e)
		{
			$e->showError();
		} 
	}

	function _sess_write($key, $val) 
	{
		global $db,$sys_conf_var;

		try 
		{
			if(!is_object($db))
			{
				$db = DBFactory::getDBConnection($sys_conf_var['db']);
			}
			
			$expiry = time() + $sys_conf_var['session']['online_hold'];
			$value = $val;
	
			$qid = "select count(*) as total
				from " . $sys_conf_var['table']['session'] . "
				where sesskey = '" . $key . "'";
	
			$total = $db->Execute($qid);
	
			if ($total->fields['total'] > 0) 
			{
				$sql = "update " . $sys_conf_var['table']['session'] . "
				set expiry = '" . $expiry . "', value = '" . $value . "'
				where sesskey = '" . $key . "'";
	
				return $db->Execute($sql);
	
			} 
			else 
			{
				$sql = "insert into " . $sys_conf_var['table']['session'] . "
				values ('" . $key . "', '" . $expiry . "', '" .
				$value . "')";
	
				return $db->Execute($sql);
			}
		}
		catch (MySqlException $e)
		{
			$e->showError();
		} 
	}

	function _sess_destroy($key) 
	{
		global $db,$sys_conf_var;
		
		try 
		{
			$sql = "delete from " . $sys_conf_var['table']['session'] . " where sesskey = '" . $key . "'";
			return $db->Execute($sql);
		}
		catch (MySqlException $e)
		{
			$e->showError();
		} 
	}

	function _sess_gc($maxlifetime) 
	{
		global $db,$sys_conf_var;
		
		try 
		{
			$sql = "delete from " . $sys_conf_var['table']['session'] . " where expiry < " . time();
			$db->Execute($sql);
			return true;
		}
		catch (MySqlException $e)
		{
			$e->showError();
		} 
	}

	session_set_save_handler('_sess_open', '_sess_close', '_sess_read', '_sess_write', '_sess_destroy', '_sess_gc');
}

function sys_session_id($session_id = '')
{
	if (!empty($session_id))
	{
		return session_id($session_id);
	} 
	else 
	{
		return session_id();
	}
}

function sys_session_name($name = '') 
{
	if (!empty($name)) 
	{
		return session_name($name);
	} 
	else 
	{
		return session_name();
	}
}

function sys_session_close() 
{
	if (function_exists('session_close')) 
	{
		return session_close();
	}
}

function sys_session_destroy() 
{
	return session_destroy();
}

function sys_session_save_path($path = '') 
{
	if (!empty($path)) 
	{
		return session_save_path($path);
	} 
	else 
	{
		return session_save_path();
	}
}

function sys_session_start()
{
	global $sys_conf_var;
	
	@ini_set('session.gc_probability', 1);
    @ini_set('session.gc_divisor', 2);
    @ini_set('session.gc_maxlifetime', $sys_conf_var['session']['online_hold']);
    
    //sys_session_save_path(CACHE_DIR . 'session/');
    
	/**
	 * set the session cookie parameters
	 */
	session_set_cookie_params(0,'/');
	
	$temp = session_start();
	
	//sys_session_name('zenid');

    if (!isset($_SESSION['securityToken'])) 
    {
      $_SESSION['securityToken'] = md5(uniqid(rand(), true));
    }
    
  	if (ereg_replace('[a-zA-Z0-9]', '', session_id()) != '') session_regenerate_id();
  	
    return $temp;
}

function checkPope($str)
{
	$permission = false;
	if (strpos($_SESSION['user_info']['level'],',' . $str . ',') !== false || strpos($_SESSION['user_info']['level'],',' . $str . '.') !== false)
	{
		$permission = true;
	}
	if (!$permission)
	{
		show_choices('您没有权限进行此项操作.');
	}
}

?>